package com.app.couponsharelife.ui.main.seeding
import android.content.Context
import android.os.Bundle
import androidx.fragment.app.Fragment
import com.app.couponsharelife.R
import com.app.couponsharelife.base.LazyFragment
import com.app.couponsharelife.ui.adapter.ViewPagerAdapter
import com.app.couponsharelife.ui.view.ScaleGeneralTitleView
import com.app.couponsharelife.utils.AppUtils
import kotlinx.android.synthetic.main.fragment_seeding.*
import net.lucode.hackware.magicindicator.ViewPagerHelper
import net.lucode.hackware.magicindicator.buildins.commonnavigator.CommonNavigator
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.CommonNavigatorAdapter
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerIndicator
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerTitleView
import net.lucode.hackware.magicindicator.buildins.commonnavigator.indicators.LinePagerIndicator

/**
 *Created by xct on 2019/5/7
 *Description:种草页面
 */
class SeedingFragment:LazyFragment() {
    private var mTitles = arrayListOf<String>()
    private var mAdapter: ViewPagerAdapter? = null
    private var commonNavigator: CommonNavigator?=null
    private var mPagers = arrayListOf<Fragment>()
    companion object {
        fun newInstance(): SeedingFragment {
            val fragment = SeedingFragment()
            fragment.arguments = Bundle()
            return fragment
        }
    }
    override val viewLayoutId: Int
        get() = R.layout.fragment_seeding

    override fun initViews() {
        initViewPager()
    }
    private fun initViewPager() {
        with(mTitles) {
            add("精选单品")
            add("好货专场")
            add("精选专场")
        }
        with(mPagers) {
            add(SingleCommodityFragment.newInstance())
            add(GoodCommodityFragment.newInstance())
            add(BoutiqueFragment.newInstance())
        }
        mAdapter = ViewPagerAdapter(childFragmentManager, mPagers, mTitles)
        seeding_viewpager.adapter = mAdapter
        seeding_viewpager.offscreenPageLimit = 3
        commonNavigator= CommonNavigator(activity)
        commonNavigator!!.isAdjustMode=true
        commonNavigator!!.adapter=object : CommonNavigatorAdapter(){
            override fun getTitleView(p0: Context, p1: Int): IPagerTitleView {
                val colorTransitionPagerTitleView= ScaleGeneralTitleView(p0)
                colorTransitionPagerTitleView.normalColor = resources.getColor(R.color.gray99)
                colorTransitionPagerTitleView.textSize=16f
                colorTransitionPagerTitleView.selectedColor = resources.getColor(R.color.black)
                colorTransitionPagerTitleView.text = mTitles[p1]
                colorTransitionPagerTitleView.setOnClickListener { seeding_viewpager.currentItem = p1 }
                return colorTransitionPagerTitleView
            }
            override fun getCount(): Int {
                return mTitles.size
            }
            override fun getIndicator(p0: Context?): IPagerIndicator? {
                val indicator= LinePagerIndicator(p0)
                indicator.mode= LinePagerIndicator.MODE_EXACTLY
                indicator.lineWidth= AppUtils.dip2px(context!!,30f).toFloat()
                indicator.lineHeight= AppUtils.dip2px(context!!,2f).toFloat()
                indicator.setColors(resources.getColor(R.color.act_main_bot_nav_select_text_color))
                return indicator
            }
        }
        seeding_tablayout.navigator=commonNavigator
        ViewPagerHelper.bind(seeding_tablayout, seeding_viewpager)
    }
    override fun lazyLoad() {

    }
    fun setPagerIndex(position:Int){
        if (mPagers.isNotEmpty()){
            seeding_viewpager.setCurrentItem(position,false)
        }
    }
}